﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Phone.Controls;
using Microsoft.Unsupported;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using System.IO;
using System.IO.IsolatedStorage;
using Microsoft.Xna.Framework.Media;
using com.google.zxing;
using System.Text;


namespace iLikeWine
{
    public partial class FindWine : PhoneApplicationPage
    {
        BitmapImage tintoImage = new BitmapImage(new Uri("/iLikeWine;component/Images/vinho_tinto.png", UriKind.Relative));
        BitmapImage brancoImage = new BitmapImage(new Uri("/iLikeWine;component/Images/vinho_branco.png", UriKind.Relative));
        BitmapImage roseImage = new BitmapImage(new Uri("/iLikeWine;component/Images/vinho_rose.png", UriKind.Relative));

        private const string conn = @"isostore:/iLikeWine.sdf";
        Wine w = new Wine();
        int i;
       

        public FindWine()
        {
            InitializeComponent();
            traz.IsEnabled = false;
        }

        private void nome1_TextChanged(object sender, TextChangedEventArgs e)
        {
            
        }
        public IList<Wine> GetWines()
        {
            IList<Wine> lista = null;
            using (var ctx = new iLikeWineDataContext(conn))
            {
                IQueryable<Wine> query = ctx.Wines;
                //IQueryable<Wine> query = from ctx in ctx.Wines select w ;
                lista = query.ToList();
            }

            return lista;
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            IList<Wine> lista = this.GetWines();
          
            foreach (Wine w in lista)
            {

                if (w.Nome.Equals(nome1.Text) || w.Ano.Equals(nome1.Text) || w.Regiao.Equals(nome1.Text))
                {
                    id.Text = w.ID.ToString();
                    nome.Text = w.Nome;
                    ano.Text = w.Ano;
                    regiao.Text = w.Regiao;
                    if (w.Tipo.Equals("Tinto"))
                    {

                        tipo.Text = "Tinto";
                        imageTipo.Source = tintoImage;

                    }
                    else if (w.Tipo.Equals("Branco"))
                    {
                        tipo.Text = "Branco";
                        imageTipo.Source = brancoImage;

                    }
                    else
                    {
                        tipo.Text = "Rosé";
                        imageTipo.Source = roseImage;
                    }
                    //tipo.Text = w.Tipo;
                    textBlock10.Text = w.Ranking + "/10";
                    w_w_w.Text = w.Www;
                    //--> FOTO
                    //image1.DataContext = w.Fotografia;

                    //<-- FOTO
                    //
                    comentario.Text = w.Comentario;


                }
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.RemoveBackEntry();
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Pesquisa por Nome ou Ano ou Região, sensível a letras maiúsculas.");
        }

        private void frente_Click(object sender, RoutedEventArgs e)
        {

            IList<Wine> lista = this.GetWines();
            i++;
            foreach (Wine w in lista)
            {

               if (w.ID.Equals(i) && (w.Nome.Equals(nome1.Text) || w.Ano.Equals(nome1.Text) || w.Regiao.Equals(nome1.Text)))
                {
                    traz.IsEnabled = true;
                    id.Text = w.ID.ToString();
                    nome.Text = w.Nome;
                    ano.Text = w.Ano;
                    regiao.Text = w.Regiao;
                    if (w.Tipo.Equals("Tinto"))
                    {

                        tipo.Text = "Tinto";
                        imageTipo.Source = tintoImage;

                    }
                    else if (w.Tipo.Equals("Branco"))
                    {
                        tipo.Text = "Branco";
                        imageTipo.Source = brancoImage;

                    }
                    else
                    {
                        tipo.Text = "Rosé";
                        imageTipo.Source = roseImage;
                    }
                    //tipo.Text = w.Tipo;
                    textBlock10.Text = w.Ranking + "/10";
                    w_w_w.Text = w.Www;
                    //--> FOTO
                    //image1.DataContext = w.Fotografia;

                    //<-- FOTO
                    //
                    comentario.Text = w.Comentario;


                }
            }

        }

        private void traz_Click(object sender, RoutedEventArgs e)
        {
            i--;
            if (i == 1) traz.IsEnabled = false;
            IList<Wine> lista = this.GetWines();

            foreach (Wine w in lista)
            {


                if (w.ID.Equals(i) && (w.Nome.Equals(nome1.Text) || w.Ano.Equals(nome1.Text) || w.Regiao.Equals(nome1.Text)))
                {
                    traz.IsEnabled = true;
                    id.Text = w.ID.ToString();
                    nome.Text = w.Nome;
                    ano.Text = w.Ano;
                    regiao.Text = w.Regiao;
                    if (w.Tipo.Equals("Tinto"))
                    {

                        tipo.Text = "Tinto";
                        imageTipo.Source = tintoImage;

                    }
                    else if (w.Tipo.Equals("Branco"))
                    {
                        tipo.Text = "Branco";
                        imageTipo.Source = brancoImage;

                    }
                    else
                    {
                        tipo.Text = "Rosé";
                        imageTipo.Source = roseImage;
                    }
                    //tipo.Text = w.Tipo;
                    textBlock10.Text = w.Ranking + "/10";
                    w_w_w.Text = w.Www;
                    //--> FOTO
                    //image1.DataContext = w.Fotografia;

                    //<-- FOTO
                    //
                    comentario.Text = w.Comentario;


                }
            }

        }
    }
}